/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package opt.Impl;

import dao.AccountDao;
import dao.Impl.AccountDaoImpl;
import dao.Impl.PetDaoImpl;
import dao.Impl.PetOwnerDaoImpl;
import dao.Impl.PetStoreDaoImpl;
import dao.PetDao;
import dao.PetOwnerDao;
import dao.PetStoreDao;
import entity.Account;
import entity.Pet;
import entity.PetOwner;
import entity.PetStore;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import opt.PetStoreService;

/**
 *
 * @author 10592
 */
public class PetStoreServiceImpl implements PetStoreService{
    Scanner scanner = new Scanner(System.in);
    List<Pet> pets;
    PetStore petStore;
    String password;
    int account;
    @Override
    public void buy(Pet pet) {
        if (petStore.getBalance()> pet.getPrice()) {
            int masterid=pet.getMasterId();
            if (petStore.getId()==masterid) {
                System.out.println("本店宠物无需购买");
            }
            else
            {
                //更新宠物信息
                PetDaoImpl petDao = new PetDaoImpl();
                 petDao.updata(pet.getId(), "店主", petStore.getId());
           
                //更新自家宠物商店信息
                 PetStoreDao instance = new PetStoreDaoImpl();
                instance.updata(petStore.getId(), petStore.getBalance() - pet.getPrice());
                //更新卖家宠物商店信息
                PetStore sellerPetStore=instance.getBtId(masterid);
                instance.updata(masterid, sellerPetStore.getBalance()+pet.getPrice());
            }
            

        } else {
            System.out.println("您的余额不足，请重新选择");
        }
    }

    @Override
    public void sell(Pet pet) {
        System.out.println("---------下面是现有宠物商店，请选择您要卖给的买家序号------------");
        PetStoreDao instance = new PetStoreDaoImpl();
        List<PetStore> petStores = instance.getlist();
        System.out.println("序号"+"\t"+"商店编号" + "\t" + "商店名称" + "\t" + "\t" + "商店金额");
        for (int i = 0; i < petStores.size(); i++) {
            System.out.print(i+1+"\t");
            System.out.print(petStores.get(i).getId() + "\t");
            System.out.print(petStores.get(i).getName() + "\t");
            System.out.print(petStores.get(i).getBalance() + "\t");
            System.out.println();
        }
        while (!scanner.hasNextInt()) {
            System.out.println("输入错误，请重新输入");
            scanner.next();
        }
        int num = scanner.nextInt();
        if (num > petStores.size()) {
            System.out.println("没有该商店。");
        } else {
            //更新宠物商店信息
            instance.updata(petStore.getId(), petStore.getBalance() + pet.getPrice());
            //得到宠物买家商店信息
            PetStore buyerPetStore=petStores.get(num-1);
            instance.updata(buyerPetStore.getId(), buyerPetStore.getBalance()-pet.getPrice());
            //更新宠物信息
            PetDaoImpl petDao = new PetDaoImpl();
            petDao.updata(pet.getId(), "店主", buyerPetStore.getId());
            //更新账单   
            try {
                Date date = new Date(System.currentTimeMillis());
                Account account = new Account(1, pet.getPrice(), pet.getId(),petStore.getId(), buyerPetStore.getId(), date);
                AccountDao accountDao = new AccountDaoImpl();
                accountDao.add(account);
            } catch (SQLException ex) {
                Logger.getLogger(PetOwnerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public List<Pet> getPetInStore(int store) {
        System.out.println("-------以下是库存宠物---------");
        PetDao instance = new PetDaoImpl();
        System.out.println("宠物编号" + "\t" + "宠物名称" + "\t" + "宠物类型" + "\t" + "宠物价格");
        pets = instance.getlist(store,"店主");
        for (int i = 0; i < pets.size(); i++) {
            System.out.println(pets.get(i).getId() + "\t" + pets.get(i).getName() + "\t" + pets.get(i).getKind() + "\t" + pets.get(i).getPrice());
        }
        return pets;
    }

    @Override
    public List<Account> getAccount(int storeID) {
        AccountDao instance = new AccountDaoImpl();
        int sellid = petStore.getId();
        int dealtype = 2;
        List<Account> accounts = instance.getlist(sellid, dealtype);
        System.out.println("账单编号" + "\t" + "账单类型" + "\t" + "宠物编号" + "\t" + "卖家编号" + "买家编号" + "\t" + "交易金额" + "\t" + "交易时间");
        for (int i = 0; i < accounts.size(); i++) {
            System.out.print(accounts.get(i).getId() + "\t");
            System.out.print(accounts.get(i).getDealType() + "\t");
            System.out.print(accounts.get(i).getPetId() + "\t");
            System.out.print(accounts.get(i).getSellerId() + "\t");
            System.out.print(accounts.get(i).getBuyerId() + "\t");
            System.out.print(accounts.get(i).getPrice() + "\t");
            System.out.print(accounts.get(i).getDealTime() + "\t");
            System.out.println();
        }
        return accounts;
    }

    @Override
    public PetStore login() {
        System.out.println("宠物商店你好，请您输入商店账号：");
        account = scanner.nextInt();
        System.out.println("宠物商店你好，请您输入商店密码：");
        password = scanner.next();
        PetStoreDao instance = new PetStoreDaoImpl();
        petStore = instance.login(account, password);
        if (petStore != null) {
            System.out.println("主人编号" + "\t" + "主人名称" + "\t" + "主人密码" + "\t" + "主人金额");
            System.out.println("-----------恭喜您登录成功--------------");
            System.out.println("-----------您的基本信息：--------------");
            System.out.println("姓名：" + petStore.getName());
            System.out.println("金额：" + petStore.getBalance());
        } else {
            System.out.println("用户名或密码错误，请重输入");
        }
        return petStore;
    }
    
}
